import axios from 'axios'
import ElementUI from 'element-ui'
import config from './config.js'
import token from './token.js'

axios.defaults.timeout = 5000
axios.defaults.baseURL = ''

// 前置拦截
axios.interceptors.request.use(config => {
  return config
})

// 后置拦截
axios.interceptors.response.use(response => {
  let re = response.data
  console.log(re)
  if (re.code === 200) {
    // 继续执行
    return response
  } else {
    // 提示message返回的内容
    ElementUI.Message.error(re.message, {duration: 3 * 1000})
  }
})

/**
 * 登录 注册 请求
 * @param url
 * @param data
 * @param type
 * @returns {Promise<any>}
 */
export function getLogin (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.request({
      url: config.BASE_URL.BASE_URL + url,
      params: data,
      method: 'GET',
      headers: {
        'content-type': 'application/json;charset=UTF-8'
      }
    }).then((res) => {
      console.log('返回数据 satoken', token.getToken())
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export function getRegister (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.request({
      url: config.BASE_URL.BASE_URL + url,
      data: data,
      method: 'POST',
      headers: {
        'content-type': 'application/json;charset=UTF-8'
      }
    }).then((res) => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

//******************文件地址请求*************************//
/**
 * 图片路径
 * @type {string}
 */
function postRequestPicture (url, data, type) {
  var promise = new Promise((resolve, reject) => {
    var postData = data
    axios.request({
      url: config.VUE_APP_RESOURCES_URL.VUE_APP_RESOURCES_URL + url,
      data: postData,
      method: 'POST',
      headers: {
        'content-type': type || 'multipart/form-data',
      },
      success: function (res) {
        resolve(res.data)
      },
      error: function (e) {
        reject('网络出错')
      }
    }).then((res) => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
  return promise
}

//******************后台地址请求*************************//

/**
 * 后台地址
 * @param url
 * @param data
 * @returns {Promise}
 */
export function systemGetRequest (url, data) {
  return new Promise((resolve, reject) => {
    axios.request({
      url: config.VUE_APP_API_URL.VUE_APP_API_URL + url,
      method: 'GET',
      params: data,
      headers: {
        'content-type': 'application/json;charset=UTF-8',
        'satoken': token.getToken()
      }
    }).then((res) => {
      resolve(res)
      console.log('返回数据', token.getToken())
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 后台地址
 * @param url
 * @param data
 * @returns {Promise}
 */
export function systemPostRequest (url, data) {
  return new Promise((resolve, reject) => {
    axios.request({
      url: config.VUE_APP_API_URL.VUE_APP_API_URL + url,
      method: 'POST',
      data: data,
      headers: {
        'content-type': 'application/json;charset=UTF-8',
        'satoken': token.getToken()
      }
    }).then((res) => {
      resolve(res)
      console.log('返回数据', token.getToken())
    }).catch(err => {
      reject(err)
    })
  })
}

//******************公用 GET POST请求*************************//

/**
 * get 请求
 * @param url
 * @param data
 * @param type
 * @returns {Promise<any>}
 */
export function getRequest (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.request({
      url: config.BASE_URL.BASE_URL + url,
      params: data,
      method: 'GET',
      headers: {
        'content-type': 'application/json;charset=UTF-8',
        'satoken': token.getToken()
      }
    }).then((res) => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * post请求封装
 * @param url
 * @param data
 * @param type
 * @returns {Promise<any>}
 */
export function postRequest (url, data = {}, type) {
  var promise = new Promise((resolve, reject) => {
    var postData = data
    axios.request({
      url: config.BASE_URL.BASE_URL + url,
      data: postData,
      method: 'POST',
      headers: {
        'content-type': type || 'application/json;charset=UTF-8',
        'satoken': token.getToken()
      }
    }).then((res) => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
  return promise
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch (url, data = {}, type = 'application/json;charset=UTF-8') {
  return new Promise((resolve, reject) => {
    axios.patch(url, {
      params: params,
      headers: {
        'Content-Type': type
      }
    })
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 更新请求
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put (url, data = {}, type = 'application/json;charset=UTF-8') {
  return new Promise((resolve, reject) => {
    axios.put(url, {
      params: params,
      headers: {
        'Content-Type': type
      }
    })
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 封装del请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function del (url, params = {}, type = 'application/json;charset=UTF-8') {
  return new Promise((resolve, reject) => {
    axios.delete(url, {
      params: params,
      headers: {
        'Content-Type': type
      }
    })
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

export default {
  postRequest,
  getRequest,
  systemGetRequest,
  systemPostRequest,
  getLogin,
  getRegister,
  postRequestPicture,
  patch, put, del
}
